/**
 * Created by HP on 2016/10/28.
 */

function timer(totalSecond){

    var timerA = setInterval(function(){

        var day=0,

            hour=0,

            minute=0,

            second=0;//时间默认值

        if(totalSecond > 0){

            day = Math.floor(totalSecond / (60 * 60 * 24));

            hour = Math.floor(totalSecond / (60 * 60)) - (day * 24);

            minute = Math.floor(totalSecond / 60) - (day * 24 * 60) - (hour * 60);

            second = Math.floor(totalSecond) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);

        }

        $(".suspend").click(function () {
            clearInterval(timerA);
        })
        if(totalSecond == 0){
            $('.botton-music').addClass('btn-mus-end');
        }

        if(totalSecond < 0){
            clearInterval(timerA);
            $.popup('.popup-result');
        }

        if (hour <= 9) hour = '0' + minute;

        if (minute <= 9) minute = '0' + minute;

        if (second <= 9) second = '0' + second;

        $('#day_show').html(day+"天");

        $('#hour_show').html('<s id="h"></s>'+hour+':');

        $('#minute_show').html('<s></s>'+minute+':');

        $('#second_show').html('<s></s>'+second+'');

        totalSecond--;

    }, 1000);

    return timerA;

}

$(document).ready(function () {
    $.init();
    var first_hour = $("#hour_show").text();
    var first_min = $("#minute_show").text();
    var first_sec = $("#second_show").text();
    $(".btn-start").click(function () {
        var totalSecond = 0;

        var org_hour = $("#hour_show").text();
        var org_min = $("#minute_show").text();
        var org_sec = $("#second_show").text();

        var hourSecond = parseInt(org_hour);
        var minSecond = parseInt(org_min);
        var Second = parseInt(org_sec);

        var timerB;

        totalSecond = hourSecond*3600 + minSecond*60 + Second;

        if($(this).hasClass("btn-start")){
            $(this).removeClass("btn-start");
            $(this).addClass("suspend");
            $(this).text("暂停");
            timer(totalSecond);
        }else{
            $(this).removeClass("suspend");
            $(this).addClass("btn-start");
            $(this).text("开始运动");

        }

    })


    $(".close-popup").click(function () {
        // var org_hour = $("#hour_show").text();
        // var org_min = $("#minute_show").text();
        // var org_sec = $("#second_show").text();
        $("#hour_show").text(first_hour);
        $("#minute_show").text(first_min);
        $("#second_show").text(first_sec);
        $(".botton-gray").removeClass('suspend');
        $(".botton-gray").addClass("btn-start");
        $(".botton-gray").text("开始运动");
        $("#hour_show").text(org_hour);
        $("#minute_show").text(org_min);
        $("#second_show").text(org_sec);
    })

})